package com.free.picture.ecognition.controller;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.image.v2.ImageClient;
import com.huaweicloud.sdk.image.v2.model.*;
import com.huaweicloud.sdk.image.v2.region.ImageRegion;
import com.free.common.core.web.domain.R;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class RecognitionController {
    @PostMapping("recognitionImages")
    public R<List<ImageTaggingItemBody>> recognitionImages(@RequestBody ImageTaggingReq body) {

        String ak = "5RZSRODMUZ7CFZHQMFTJ";
        String sk = "ffPWO2sjn70cT4JvsbbUqyksLv8g1YTieUoK08TC";

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);

        ImageClient client = ImageClient.newBuilder()
                .withCredential(auth)
                .withRegion(ImageRegion.valueOf("cn-north-4"))
                .build();
        RunImageTaggingRequest request = new RunImageTaggingRequest();
        request.withBody(body);
        try {
            RunImageTaggingResponse response = client.runImageTagging(request);
            response.getResult().getTags().forEach(e -> {
                System.out.println(e.toString());
            });
            return R.ok(response.getResult().getTags());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
        return R.fail();
    }





}
